/*
 * $Id: AbstractFormController.java,v 1.7 2009/07/21 16:58:20 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.Assert;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.dbLogger.ContactLogger;
import com.ge.healthcare.autosc.onwatch.bean.BaseInfoBean;
import com.ge.healthcare.autosc.onwatch.service.IOnWatchService;
import com.ge.healthcare.autosc.onwatch.util.OnWatchConstants;
import com.ge.healthcare.autosc.onwatch.util.ResourceBundleUtil;
/**
 * Abstract Controller for all Onwatch controller.
 * @author 212042946
 *
 */
public abstract class AbstractFormController extends SimpleFormController {
	//private MessageSource messageSource;
	private IOnWatchService onWatchService = null;
	private ICodeDetailsDAO codeDetailsDAO = null;
	
	/**
	 * Init method (Spring init method)
	 */
	protected void init() {
		//Assert.notNull(messageSource, "MessageSource object is not defined. " + 
		//		"Please use setMessageSource to provide one");
		Assert.notNull(onWatchService, "onWatchService object is not defined. " + 
		"Please use setOnWatchService to provide one");
		Assert.notNull(codeDetailsDAO, "CodeDetailsDAO object is not defined. " + 
		"Please use setCodeDetailsDAO to provide one");
	}
	
	/**
	 * Set CodeDetailsDAO
	 * @param codeDetailsDAO
	 */
	public void setCodeDetailsDAO(ICodeDetailsDAO codeDetailsDAO) {
		this.codeDetailsDAO = codeDetailsDAO;
	}
	
	/**
	 * Get CodeDetailsDAO
	 * @return	ICodeDetailsDAO
	 */
	protected ICodeDetailsDAO getCodeDetailsDAO() {
		return codeDetailsDAO;
	}
	
	/**
	 * Set OnwatchService
	 * @param onWatchService
	 */
	public void setOnWatchService(IOnWatchService onWatchService) {
		this.onWatchService = onWatchService;
	}
	
	/**
	 * Get OnwatchService
	 * @return
	 */
	protected IOnWatchService getOnWatchService() {
		return onWatchService;
	}
	
	/**
	 * Log HTTP error response 
	 * @param errors    List of errors
	 */
	public void logErrors(Errors errors) {
		String methodName = "logErrors";
		List<ObjectError> errorList = errors.getAllErrors();
		Iterator<ObjectError> errIter = errorList.iterator();
		while(errIter.hasNext()) {
			ObjectError err = (ObjectError)errIter.next();
			ASCLogger.warn(this.getClass(), methodName,
					"error: " + ResourceBundleUtil.getMessage(
							OnWatchConstants.ONWATCH_MESSAGE_BASENAME, 
							err.getCode(), 
							err.getArguments(), 
							err.getDefaultMessage(), 
							null));
		}
	}
	
	/**
	 * This method calls showForm and gets View for error response 
	 * @param request
	 * @param response
	 * @param errors
	 * @return	ModelAndView object
	 * @throws ServletException
	 */
	protected ModelAndView callError(HttpServletRequest request, 
		HttpServletResponse response, BindException errors) 
		throws ServletException {
		String methodName = "callShowForm";
		ModelAndView mav = null;
		try {
			mav = showForm(request, response, errors);
		} catch (Exception e) {
			ASCLogger.error(this.getClass(), methodName, 
					"Failed creating application response to the requset", e);
			throw new ServletException(
					"Failed creating application response to the request", e);
		}
		return mav;
	}
	
	/**
	 * Check if request is from simulator. 
	 * @param bean
	 */
	protected void checkSimulatorRequest(BaseInfoBean bean) {
		String systemId = bean.getSystemId();
		if(!ASCUtil.isNullOrEmpty(systemId)) {
	        int colon = systemId.indexOf(':');
	        if (colon >= 0) 
	        {
	            String realSystemId = systemId.substring(0, colon);
	            String ip = systemId.substring(colon+1);
	            ContactLogger.addContactLog("simulate ip " + ip + " and systemId " + realSystemId);
	            //bean.setUsn(realUsn);
	            bean.setSystemId(realSystemId);
	            bean.setRemoteAddr(ip);
	        }
		}
	}
}
